/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerGUI.java
 *
 * Created on Apr 17, 2011, 1:50:59 PM
 */

package com.swe622.rmi.server.fileserver.gui;

import java.io.File;
import java.net.InetAddress;
import java.util.List;
import picshareservertest.ServerControl;
import picshareservertest.ServerListener;
import shared.ServerInfo;
import tcpserver.ConnectionListener;
import tcpserver.ConnectionManager;
import tcpserver.ServerEvent;

/**
 *
 * @author Melanie
 */
public class ServerGUI extends javax.swing.JFrame implements ConnectionListener, ServerListener{

    /** Creates new form ServerGUI */
    public ServerGUI() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        serverNameLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        uploadingTxtArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        hostedFilesTxtArea = new javax.swing.JTextArea();
        errLbl = new javax.swing.JLabel();
        statusLabel = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        statusTxtArea = new javax.swing.JTextArea();
        statusLabel1 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        errorTxtArea1 = new javax.swing.JTextArea();
        jScrollPane5 = new javax.swing.JScrollPane();
        downloadingTxtArea = new javax.swing.JTextArea();
        uploadingLabel = new javax.swing.JLabel();
        uploadingLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("American Typewriter", 1, 18));
        jLabel1.setForeground(new java.awt.Color(51, 153, 255));
        jLabel1.setText("Server Name:");

        serverNameLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        serverNameLabel.setText("Server Name Here");

        uploadingTxtArea.setColumns(20);
        uploadingTxtArea.setEditable(false);
        uploadingTxtArea.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        uploadingTxtArea.setRows(5);
        uploadingTxtArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jScrollPane1.setViewportView(uploadingTxtArea);

        hostedFilesTxtArea.setColumns(20);
        hostedFilesTxtArea.setEditable(false);
        hostedFilesTxtArea.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        hostedFilesTxtArea.setRows(5);
        hostedFilesTxtArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        hostedFilesTxtArea.setOpaque(false);
        jScrollPane2.setViewportView(hostedFilesTxtArea);

        errLbl.setText("Server Errors");

        statusLabel.setText("Status");

        statusTxtArea.setColumns(20);
        statusTxtArea.setEditable(false);
        statusTxtArea.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        statusTxtArea.setRows(5);
        statusTxtArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jScrollPane3.setViewportView(statusTxtArea);

        statusLabel1.setText("Hosted Files");

        errorTxtArea1.setColumns(20);
        errorTxtArea1.setEditable(false);
        errorTxtArea1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        errorTxtArea1.setRows(5);
        errorTxtArea1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jScrollPane4.setViewportView(errorTxtArea1);

        downloadingTxtArea.setColumns(20);
        downloadingTxtArea.setEditable(false);
        downloadingTxtArea.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        downloadingTxtArea.setRows(5);
        downloadingTxtArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jScrollPane5.setViewportView(downloadingTxtArea);

        uploadingLabel.setText("Uploading Clients");

        uploadingLabel1.setText("Downloading Clients");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(47, 47, 47)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(statusLabel)
                            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 366, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                            .add(statusLabel1)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 249, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jScrollPane5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 249, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(uploadingLabel1)
                            .add(uploadingLabel)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(24, 24, 24)
                                .add(errLbl))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jLabel1)
                                .add(18, 18, 18)
                                .add(serverNameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 312, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                .addContainerGap(45, Short.MAX_VALUE))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(46, 46, 46)
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 367, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(307, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(serverNameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(errLbl)
                    .add(uploadingLabel))
                .add(7, 7, 7)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 77, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(uploadingLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 78, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusLabel1)
                    .add(statusLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane2)
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE))
                .addContainerGap(43, Short.MAX_VALUE))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(73, 73, 73)
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(291, Short.MAX_VALUE)))
        );

        jPanel2.setBackground(new java.awt.Color(153, 51, 255));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 105, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 552, Short.MAX_VALUE)
        );

        jPanel3.setBackground(new java.awt.Color(153, 51, 255));

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 105, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 552, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ServerGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea downloadingTxtArea;
    private javax.swing.JLabel errLbl;
    private javax.swing.JTextArea errorTxtArea1;
    private javax.swing.JTextArea hostedFilesTxtArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JLabel serverNameLabel;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JLabel statusLabel1;
    private javax.swing.JTextArea statusTxtArea;
    private javax.swing.JLabel uploadingLabel;
    private javax.swing.JLabel uploadingLabel1;
    private javax.swing.JTextArea uploadingTxtArea;
    // End of variables declaration//GEN-END:variables

    public void serverConnectionManagerErrorOccurred(ServerEvent e) {
        errorTxtArea1.append(e.getMessage());
    }

    public void serverConnectionEvent(ServerEvent e) {
        statusTxtArea.append(e.getMessage()+"\n");
        
    }

    public void hostedFilesUpdatedEvent(ServerEvent e) {
        ServerControl control = (ServerControl)e.getSource();
        String fileListString = "";
        synchronized(control.getHostedFileList())
        {
              for(File file : control.getHostedFileList())
                {
                    fileListString += file.getName() + "\n";
                }
        }
      
        hostedFilesTxtArea.setText(fileListString);
        
    }

    public void serverInfoUpdatedEvent(ServerEvent e) {
         ServerControl control = (ServerControl)e.getSource();
         ServerInfo info = control.getServerInfo();
         if (info.getAddress()!=null)
             serverNameLabel.setText(info.getAddress().getHostName());
         else
             serverNameLabel.setText("<Unknown>");
    }

    public void serverErrorEvent(ServerEvent e) {
        errorTxtArea1.setText(e.getMessage()+ "\n");
    }

    public void uploadClientListChanged(ServerEvent e) {

       ConnectionManager manager = (ConnectionManager)e.getSource();
      
    }
    public void downloadClientListChanged(ServerEvent e) {

       
       
    }

    public String formatInetList(List<InetAddress> list)
    {
        String uploadList = "";
        for(InetAddress addr : list)
           uploadList += "Name: " + addr.getHostName() + " Address: " + addr.getHostAddress()+"\n";
        return uploadList;
    }

    public void transferListChanged(ServerEvent e) {
        ConnectionManager manager = (ConnectionManager)e.getSource();
        List<InetAddress> downloading = manager.getClientDownloadList();
        downloadingTxtArea.setText(formatInetList(downloading));
        List<InetAddress> uploading = manager.getClientUploadList();
        uploadingTxtArea.setText(formatInetList(uploading));
    }
}
